import java.util.HashSet;
import java.util.Set;

public class Solution679 {

    boolean flag=false;
    private static final double m = 0.001;

    public boolean judgePoint24(int[] cards) {
        int[] temp=new int[4];
        int[] visit=new int[4];
        dfs(0,temp,visit,cards);
        return flag;
    }

    private void dfs(int k, int[] temp, int[] visit, int[] cards) {
        if (k==4){
            check(temp);
            return;
        }
        for (int i=0;i<4;i++){
            if (visit[i]==0){
                temp[k]=cards[i];
                visit[i]=1;
                dfs(k+1,temp,visit,cards);
                if (flag){
                    return;
                }
                visit[i]=0;
            }
        }
    }

    private void check(int[] t) {
        Set<Double> set=calThree(t,1,3);
        for (Double aDouble : set) {
            if (Math.abs(t[0]+aDouble-24)<m || Math.abs(t[0]-aDouble-24)<m || Math.abs(t[0]*aDouble-24)<m || aDouble!=0 && Math.abs(t[0]/aDouble-24)<m){
                flag=true;
                return;
            }
        }
        set=calThree(t,0,2);
        for (Double aDouble : set) {
            if (Math.abs(aDouble+t[3]-24)<m || Math.abs(aDouble-t[3]-24)<m || Math.abs(t[3]*aDouble-24)<m || t[3]!=0 && Math.abs(aDouble/t[3]-24)<m){
                flag=true;
                return;
            }
        }
        set=calTwo(t,0,1);
        Set<Double> set2=calTwo(t,2,3);
        for (Double aDouble : set) {
            for (Double aDouble1 : set2) {
                if (Math.abs(aDouble+aDouble1-24)<m || Math.abs(aDouble-aDouble1-24)<m || Math.abs(aDouble1*aDouble-24)<m || aDouble1!=0 && Math.abs(aDouble/aDouble1-24)<m){
                    flag=true;
                    return;
                }
            }
        }
    }

    private Set<Double> calThree(int[] t, int start, int end) {
        Set<Double> ans=new HashSet<>();
        Set<Double> temp=calTwo(t,start+1,end);
        for (Double aDouble : temp) {
            ans.add(t[start]+aDouble);
            ans.add(t[start]-aDouble);
            ans.add(t[start]*aDouble);
            if (aDouble!=0) {
                ans.add(t[start] / aDouble);
            }
        }
        temp=calTwo(t,start,end-1);
        for (Double aDouble : temp) {
            ans.add(aDouble+t[end]);
            ans.add(aDouble-t[end]);
            ans.add(t[end]*aDouble);
            if (t[end]!=0) {
                ans.add(aDouble/t[end]);
            }
        }
        return ans;
    }

    private Set<Double> calTwo(int[] t, int a, int b) {
        Set<Double> ans=new HashSet<>();
        ans.add(t[a]+t[b]*1.0);
        ans.add(t[a]-t[b]*1.0);
        ans.add(t[a]*t[b]*1.0);
        if (t[b]!=0){
            ans.add(1.0*t[a]/t[b]);
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution679().judgePoint24(new int[]{1,2,1,2}));
    }
}
